<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      
      <div class='logo_container'>
        <img class='img_logo'>
        <div class='title_logo'>汽修后台管理系统</div>
      </div>
      <!-- <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div> -->

      <!-- <el-select v-model="listQuery.importance" placeholder="Imp" clearable style="width: 90px" class="filter-item">
        <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
      </el-select> -->
      
     <!-- <el-form-item >
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
         <el-select  v-model="optionText"  clearable  class="el-select">
            <el-option v-for="item in optionsData" :key="item" :label="item" :value="item" />
         </el-select>
      </el-form-item> -->

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="loginForm.username"
          placeholder="账户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
          unselectable="on"
        />
        
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="账户密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary"  class='btn_login'  @click.native.prevent="handleLogin">登录</el-button>

      <!-- <div class="tips">
        <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span>
      </div> -->

    </el-form>
  </div>
</template>

<script>
// import {login} from '@/api/user'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback('请输入用户名')
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback('密码请输入不小于6的数字,字母')
      } else {
        callback()
      }
    }
    return {
      list: '',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      optionsData:['总部','门店'],
      optionText:''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // this.$refs.loginForm.validate(valid => {
        // if (valid) {
          this.loading = true
          const paramsData ={
                phone:this.loginForm.username,
                password:this.loginForm.password
          }
          // this.$router.push({ path:  '/' })
          this.$store.dispatch('user/login', this.loginForm).then((response) => {
            // console.log(response)
            if(response.code==200) {
              // console.log(response.data)
              this.$store.dispatch('user/getUserInfo', response.data)
              this.$router.push({ path:  '/' })
              this.$message({
                  message: '登陆成功',
                  type: 'success'
                });
            } else if(response.code==400){
                this.$message.error('账号或密码错误')
                this.loginForm.username = ''
                this.loginForm.password= ''
                this.loading = false
            }
            // this.loading = false
          }).catch(() => {
            this.loading = false
          })
        // } else {
          // console.log('登陆失败!')
          // return false
        // }
    //  })
    }
  },
  created(){
　　　　　 //select中初始化默认选中
          this.optionText = this.optionsData[0];
      },
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #555;
$inputFontColor:#979797;
$inputBorderColor:#ccc;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .logo_container{
    width:100%;
    height:180px;
    text-align:center;
    margin-bottom:20px;
    
    .img_logo{
        width:120px;
        height:120px;
        border:none;
        background:#eee;
        margin-bottom:20px;
        border-radius:10px;
        
    }
    .title_logo{
       font-size:1.2em;
       color:#555;
    }
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input{
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $inputFontColor;
      height: 47px;
      caret-color: $cursor;
      font-size:1.2em;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

   .el-select{
      display: inline-block;
      height: 47px;
       width: 92%;
        
        .el-input,
          .el-input--suffix{
             width:100%;
          }

        select{
          background: transparent;
          border: 0px;
          -webkit-appearance: none;
          border-radius: 0px;
          padding: 12px 5px 12px 15px;
          color: $light_gray;
          height: 47px;
          caret-color: $cursor;
          
          

          &:-webkit-autofill {
            box-shadow: 0 0 0px 1000px $bg inset !important;
            -webkit-text-fill-color: $cursor !important;
          }
          
       }
   }

  .el-form-item {
    border: 1px solid $inputBorderColor;
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .btn_login{
    height:45px;
    width:100%;
    line-height:20px;
    border:none;
    background:#f55d54;
    font-size:1.2em;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: #fff;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
